package com.wzt.lianfirecontrol.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.wzt.lianfirecontrol.activity.BaseActivity;

/**
 * Created by zf-note on 2017/7/13.
 */
public class GlideQmmUtil {

    public static void initGlide(Context context, String imgUrl, ImageView view) {
        if (isContinue(context, imgUrl, view)) {
            Glide.with(context).load(imgUrl).into(view);
        }
    }

    public static void initGlideWithLoadingListening(Context context, String imgUrl, ImageView view,
                                                     RequestListener requestListener, RequestOptions options) {
        if (null == requestListener) {
            initGlide(context, imgUrl, view);
            return;
        }
        if (isContinue(context, imgUrl, view)) {
            if (null == options) {
                Glide.with(context).load(imgUrl).listener(requestListener).into(view);
            }
            else {
                Glide.with(context).load(imgUrl).listener(requestListener).apply(options).into(view);
            }
        }
    }

    public static void initHoldGlide(Context context, String imgUrl, final ImageView view,
                                     RequestOptions options) {
        loadUrl(context, imgUrl, options, view);
    }

    public static void initHoldSizeGlide(Context context, String imgUrl, ImageView view,
                                         RequestOptions options) {
        loadUrl(context, imgUrl, options, view);
    }

    public static void initCircleGlide(Context context, String imgUrl, ImageView view, RequestOptions options) {
        loadUrl(context, imgUrl, options, view);
    }

    public static void initGlideForParamsCenterCrop(Context context, String imgUrl, ImageView view,
                                                   RequestOptions options) {
        loadUrl(context, imgUrl, options, view);
    }

    public static RequestOptions getGlideOptions(int width, int height, int placeHoldId,
                                                  int errorImageId, boolean isCircle) {
        RequestOptions options = new RequestOptions();
        if (placeHoldId > 0) {
            options.placeholder(placeHoldId);
        }
        if (errorImageId > 0) {
            options.error(errorImageId);
        }
        if (width > 0 && height > 0) {
            options.override(width, height);
        }
        options.format(DecodeFormat.PREFER_RGB_565);
        if (isCircle) {
            options.circleCrop();
        } else {
            options.fitCenter();
        }

        options.priority(Priority.LOW);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
/*        options.dontAnimate();
        options.dontTransform();*/
        return options;
    }

    public static RequestOptions getGlideOptions(boolean isCircle) {
        RequestOptions options = new RequestOptions();
//        options.placeholder(R.drawable.small_loadpic_empty_listpage);
//        options.error(R.drawable.small_loadpic_empty_listpage);
        options.format(DecodeFormat.PREFER_RGB_565);
        if (isCircle) {
            options.circleCrop();
        } else {
            options.fitCenter();
        }

        options.priority(Priority.LOW);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
/*        options.dontAnimate();
        options.dontTransform();*/
        return options;
    }

    public static RequestOptions getGlideOptions(boolean isCircle, int drawable) {
        RequestOptions options = new RequestOptions();
        options.placeholder(drawable);
        options.error(drawable);
        options.format(DecodeFormat.PREFER_RGB_565);
        if (isCircle) {
            options.circleCrop();
        } else {
            options.fitCenter();
        }

        options.priority(Priority.LOW);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
/*        options.dontAnimate();
        options.dontTransform();*/
        return options;
    }

    public static RequestOptions getGlideOptionsWithCenterCrop(int width, int height, int placeHoldId,
                                                                int errorImageId, boolean isCircle) {
        RequestOptions options = new RequestOptions();
        if (placeHoldId > 0) {
            options.placeholder(placeHoldId);
        }
        if (errorImageId > 0) {
            options.error(errorImageId);
        }
        if (width > 0 && height > 0) {
            options.override(width, height);
        }
        options.format(DecodeFormat.PREFER_RGB_565);
        if (isCircle) {
            options.circleCrop();
        }
        options.priority(Priority.LOW);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        options.centerCrop();
        options.dontAnimate();
        options.dontTransform();
        return options;
    }


    public static RequestOptions getGlideOptionsWithCenterCrop(boolean isCircle) {
        RequestOptions options = new RequestOptions();
//        options.placeholder(R.drawable.small_loadpic_empty_listpage);
//        options.error(R.drawable.small_loadpic_empty_listpage);
        options.format(DecodeFormat.PREFER_RGB_565);
        if (isCircle) {
            options.circleCrop();
        }
        options.priority(Priority.LOW);
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        options.centerCrop();
        options.dontAnimate();
        options.dontTransform();
        return options;
    }

    private static boolean isContinue(Context context, String imgUrl, ImageView view) {
        if (StringUtils.isEmpty(imgUrl)) {
            return false;
        }

        if (null == view) {
            return false;
        }

        if (null == context) {
            return false;
        }

        if (context instanceof BaseActivity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (((BaseActivity)context).isDestroyed()) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 加载图片到Imageview
     * @param context
     * @param imgUrl
     * @param options
     * @param view
     */
    private static void loadUrl(Context context, String imgUrl, RequestOptions options, final ImageView view) {
        if (isContinue(context, imgUrl, view)) {
            //保存Imageview图片显示type
            final ImageView.ScaleType scaleType = view.getScaleType();
            if (scaleType != ImageView.ScaleType.CENTER_INSIDE) {
                //设置占位图显示type
                view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            }
            Glide.with(context).load(imgUrl).listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    //图片加载成功，重新设置图片显示type
                    view.setScaleType(scaleType);
                    return false;
                }
            }).apply(options).into(view);
        }
    }
}
